import { http } from "@/utils/http";
import type { ConfigMap, ConfigMapList, DeleteStatus } from "./types";
import type { BaseResponse, PDataRes, PromiseRes } from "@/api/response";
import {
  k8sPathMergeReqs,
  type MakePathReq,
  type MergePathReq
} from "./apiHelper";

export class K8sConfigMap {
  baseReq: MakePathReq = {
    src: "configmaps",
    app: ""
  };

  //
  getConfigMap = (req: MergePathReq): BaseResponse<ConfigMapList> => {
    const api = k8sPathMergeReqs(req, this.baseReq);
    return http.get(api);
  };

  //
  getConfigMapDetail = (
    req: MergePathReq,
    data: ConfigMap
  ): BaseResponse<ConfigMap> => {
    const api = k8sPathMergeReqs(req, this.baseReq, {
      srcName: data.metadata?.name,
      namespace: data.metadata?.namespace
    });
    return http.get(api);
  };

  //
  updateConfigMap = (req: MergePathReq, data: ConfigMap): BaseResponse => {
    const api = k8sPathMergeReqs(req, this.baseReq, {
      namespace: data.metadata?.namespace,
      srcName: data.metadata?.name
    });
    return http.put(api, { data });
  };

  //
  deleteConfigMap = (
    req: MergePathReq<ConfigMap>,
    data: ConfigMap
  ): BaseResponse<DeleteStatus> => {
    const api = k8sPathMergeReqs(req, this.baseReq);
    return http.delete(api, { data });
  };

  //
  createConfigMap = (req: MergePathReq, data: ConfigMap): BaseResponse => {
    const api = k8sPathMergeReqs(req, this.baseReq);
    return http.post(api, { data });
  };
}
